package com.qk.controller;

import com.qk.common.Result;
import com.qk.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

  /*  @PostMapping("/upload")
    public Result upload(String userName, Integer age, MultipartFile file) throws Exception {
        log.info("用户名：{}，年龄：{}，文件：{}", userName, age, file);
        //创建一个不重复的文件名  ---> 使用UUID
        //获取原始文件名
        String fileName = file.getOriginalFilename();
        //获取文件后缀名
        String entName = fileName.substring(fileName.lastIndexOf("."));
        //拼接新的文件名
        String newName = UUID.randomUUID().toString() + entName;
        file.transferTo(new File("D:\\笔记\\" + newName));
        return Result.success();
    }*/

    /**
     * 文件上传
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws Exception {
        log.info("上传文件: {}", image);
        //创建一个不重复的文件名  ---> 使用UUID
        //获取原始文件名
        String fileName = image.getOriginalFilename();
        //获取文件后缀名
        String entName = fileName.substring(fileName.lastIndexOf("."));
        //拼接新的文件名
        String newName = UUID.randomUUID().toString() + entName;

        String url = aliyunOSSOperator.upload(image.getBytes(), newName);
        return Result.success(url);
    }

}
